package com.example.myrequestbody.controller;

import cn.hutool.core.lang.tree.TreeUtil;
import com.example.myrequestbody.annotation.MyRequestBody;
import com.example.myrequestbody.common.constant.ErrorCodeEnum;
import com.example.myrequestbody.model.School;
import com.example.myrequestbody.model.Student;
import com.example.myrequestbody.model.User;
import com.example.myrequestbody.utlis.MyValidationUtil;
import com.example.myrequestbody.validator.AddGroup;
import com.google.common.collect.Maps;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("Test")
public class TestController {

    /**
     * {
     *     "school": {
     *         "id": 1,
     *         "schoolName": "清华北大不如胆子大",
     *         "schoolAddress": "清华北大门口",
     *         "schoolArea": 120000,
     *         "schoolRating": "AAAAA"
     *     },
     *     "student": {
     *         "id": 1,
     *         "studentName": "段小杜",
     *         "className": "1024班",
     *         "grade": "七年级",
     *         "age": 25
     *     }
     * }
     * @param school
     * @param student
     * @return
     */
    @PostMapping("multipleObjects")
    public Map<String,Object> multipleObjects(@MyRequestBody School school
            ,@MyRequestBody Student student){
        HashMap<String, Object> objectObjectHashMap = Maps.newHashMap();
        //判断对象是否为空
        if (MyValidationUtil.existBlankArgument(school, student)) {
             objectObjectHashMap.put("error",ErrorCodeEnum.ARGUMENT_NULL_EXIST);
             return objectObjectHashMap;
        }
        //判断对象值中是否不正确
        String errorMessage = MyValidationUtil.getModelValidationError(school, AddGroup.class);
        if (errorMessage != null) {
            objectObjectHashMap.put("error",ErrorCodeEnum.DATA_VALIDATED_FAILED);
            objectObjectHashMap.put("msg",errorMessage);
            return objectObjectHashMap;
        }
        errorMessage = MyValidationUtil.getModelValidationError(student, AddGroup.class);
        if (errorMessage != null) {
            objectObjectHashMap.put("error",ErrorCodeEnum.DATA_VALIDATED_FAILED);
            objectObjectHashMap.put("msg",errorMessage);
            return objectObjectHashMap;
        }
        objectObjectHashMap.put("school",school);
        objectObjectHashMap.put("student",student);
        return objectObjectHashMap;
    }
    @PostMapping("singleObject")
    public Map<String,Object> singleObject(@MyRequestBody User user){
        HashMap<String, Object> objectObjectHashMap = Maps.newHashMap();
        objectObjectHashMap.put("user",user);
        return objectObjectHashMap;
    }

    @PostMapping("multipleBasicObjects")
    public Map<String,Object> multipleBasicObjects(@MyRequestBody String userId,@MyRequestBody Date processingTime){
        HashMap<String, Object> objectObjectHashMap = Maps.newHashMap();
        objectObjectHashMap.put("userId",userId);
        objectObjectHashMap.put("processingTime",processingTime);
        return objectObjectHashMap;
    }

    @PostMapping("singleBasicObject")
    public Map<String,Object> singleBasicObject(@MyRequestBody String studentId){
        HashMap<String, Object> objectObjectHashMap = Maps.newHashMap();
        objectObjectHashMap.put("studentId",studentId);
        return objectObjectHashMap;
    }
}
